package leetcode.biweekly.week82;

import java.util.Stack;

//Solution4Test
public class Solution4 {
	// Solution4Test

	public int validSubarraySize(int[] nums, int threshold) {
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < nums.length; i++) {
			while (!stack.isEmpty() && nums[stack.peek()] >= nums[i]) {
				int cur = stack.pop();
				int l = stack.isEmpty() ? 0 : stack.peek();
				int k = i - l - 1;
				if (k > 0 && nums[cur] > threshold / k) {
					return k;
				}
			}
			stack.add(i);
		}
		while (!stack.isEmpty()) {
			int cur = stack.pop();
			int l = stack.isEmpty() ? -1 : stack.peek();
			int k = nums.length - l - 1;
			if (k > 0 && nums[cur] > threshold / k) {
				return k;
			}
		}
		return -1;
	}

}
